﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLToolkit.Data;
using BLToolkit.Mapping;
using BLToolkit.DataAccess;
using BLToolkit.Data.Sql.SqlProvider;
using System.Runtime.Serialization;
using nwrole.toolbox.sync.Utils;

namespace nwrole.toolbox.sync.entity
{
    [TableName("SyncEntityColumn")]
    public class SyncEntityColumn
    {
        [DataMember]
        [PrimaryKey, Identity, SequenceName("nwrole")]
        public int Id { get; set; }

        [DataMember]
        [MapField("SyncEntityId")]
        public int SyncEntityId { get; set; }

        [DataMember]
        [MapField("SortIndex")]
        public int SortIndex { get; set; }

        [DataMember]
        [MapField("ColumnName")]
        public string ColumnName { get; set; }

        [DataMember]
        [MapField("CSharpType")]
        public string CSharpType { get; set; }

        [DataMember]
        [MapField("SourceSqlTypeName")]
        public String SourceSqlTypeName { get; set; }

        [DataMember]
        [MapField("SourceSqlTypeNullable")]
        public int SourceSqlTypeNullable { get; set; }

        [DataMember]
        [MapField("SyncEnabled")]
        public int SyncEnabled { get; set; }

        [MapIgnore]
        public bool SyncEnabledBool
        {
            get
            {
                return SyncEnabled == 1 ? true : false;
            }
            set
            {
                if (value) SyncEnabled = 1; else SyncEnabled = 0;
            }
        }

        [DataMember]
        [MapField("DestSqlTypeName")]
        public String DestSqlTypeName { get; set; }

        [DataMember]
        [MapField("DestSqlTypeNullable")]
        public int DestSqlTypeNullable { get; set; }

        [DataMember]
        [MapField("PrimaryKey")]
        public bool PrimaryKey { get; set; }

        [MapIgnore]
        [Association(ThisKey = "SyncEntityId", OtherKey = "Id", CanBeNull = false)]
        public SyncEntity SyncEntity { get; set; }

        [DataMember]
        [MapField("SourceSqlTypeMaxLength")]
        public int SourceSqlTypeMaxLength { get; set; }

        [DataMember]
        [MapField("SourceSqlColumnId")]
        public int SourceSqlColumnId { get; set; }

        [DataMember]
        [MapField("DestSqlTypeMaxLength")]
        public int DestSqlTypeMaxLength { get; set; }

        [DataMember]
        [MapField("DestSqlTypePrecision")]
        public int DestSqlTypePrecision { get; set; }

        [DataMember]
        [MapField("DestSqlTypeScale")]
        public int DestSqlTypeScale { get; set; }

        [MapIgnore]
        public bool SourceSqlTypeNullableBool
        {
            get
            {
                return SourceSqlTypeNullable == 1;
            }
            set
            {
                SourceSqlTypeNullable = (value) ? 1 : 0;
            }
        }

        [MapIgnore]
        public string SyncEnabledString
        {
            get
            {
                if (SyncEnabled == 1) return "True";
                return "False";
            }
        }

        [MapIgnore]
        public bool DestSqlTypeNullableBool
        {
            get
            {
                return DestSqlTypeNullable == 1;
            }
            set
            {
                DestSqlTypeNullable = (value) ? 1 : 0;
            }
        }

        private static Dictionary<long, bool> dict;
        public static bool ToggleSyncEnabled(long id) {
            if (dict==null) dict = new Dictionary<long,bool>();
            if (!dict.Keys.Contains(id)) {
                dict.Add(id, false);
            }
            dict[id] = !dict[id];
            return dict[id];
        }

        public void Import(SyncEntityColumn importCol)
        {
            // this.SyncEnabled = importCol.SyncEnabled;
            // this.Id = importCol.Id;
            this.SortIndex = importCol.SortIndex;
            this.ColumnName = importCol.ColumnName;
            this.CSharpType = importCol.CSharpType;

            this.SourceSqlTypeName = importCol.SourceSqlTypeName;


            this.SourceSqlTypeNullable = importCol.SourceSqlTypeNullable;
            this.SourceSqlTypeMaxLength = importCol.SourceSqlTypeMaxLength;
            this.SourceSqlColumnId = importCol.Id;

            this.DestSqlTypeName = importCol.SourceSqlTypeName;
            this.DestSqlTypeNullable = importCol.SourceSqlTypeNullable;
            this.DestSqlTypeMaxLength = importCol.SourceSqlTypeMaxLength;

            this.DestSqlTypePrecision = importCol.DestSqlTypePrecision;
            this.DestSqlTypeScale = importCol.DestSqlTypeScale;

            this.CSharpType = DbTypeUtil.MakeCSharpType(this.SourceSqlTypeName);

            this.PrimaryKey = importCol.PrimaryKey;
        }
        
        public string GetSchema()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(ColumnName);
            sb.Append(";");
            sb.Append(CSharpType);
            return sb.ToString();
        }
    }
}

